'use strict';
import {
	userInfo,
	wxLogin
} from 'common/api'

import $store from './store'

class Common {
	constructor(arg) {

	}

	/**
	 * @description 全球唯一 guid
	 */
	guid() {
		return (this.__s4() + this.__s4() + "-" + this.__s4() + "-" + this.__s4() + "-" + this.__s4() + "-" + this
			.__s4() +
			this.__s4() + this.__s4());
	}

	__s4() {
		return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
	}

	/**
	 * @description 消息提示 toast
	 */
	msg(title, duration = 1500, mask = false, icon = 'none') {
		if (!title) return;

		uni.showToast({
			title,
			duration,
			mask,
			icon
		});
	}

	/**
	 * @description 返回上一级页面|跳转首页
	 * */
	back() {

		if (getCurrentPages().length > 1) {
			uni.navigateBack({})
			return
		}
		uni.navigateTo({
			url: '/pages/info/info'
		});

	}

	/**
	 * 跳转页面
	 */
	jumpTo(url) {
		uni.navigateTo({
			url: url,
			fail(error) {
				console.log(error)
			}
		})
	}

	/**
	 * 刷新当前页面
	 */
	reloadPage() {

		// #ifdef H5
		window.location.reload()
		return
		// #endif

		let pages = getCurrentPages();
		let page = pages[pages.length - 1];
		let options = page.options || {};
		let route = page.route || '';
		if (route.indexOf('/') !== 0) {
			route = '/' + route;
		}
		let query = '';
		for (let k in options) {
			query = query + `${k}=${options[k]}&`;
		}
		uni.redirectTo({
			url: route + (query ? `?${query}` : ``),
		});
	}

	/**
	 * 复制
	 */
	copyText(value) {
		uni.setClipboardData({
			data: `${value}`,
			success: function() {
				uni.showToast({
					title: '复制成功~',
					icon: 'sucesss'
				})
			},
			fail: function(err) {
				uni.showToast({
					title: '复制失败~',
					icon: 'none'
				})
			}
		});
	}

	/**
	 *
	 * @param {缓存key} key
	 * @param {需要存储的缓存值} value
	 * @param {过期时间，默认0表示永久有效} expire
	 */
	setCache(key, value, expire = 0) {
		let obj = {
			data: value, //存储的数据
			time: Date.now() / 1000, //记录存储的时间戳
			expire: expire //记录过期时间，单位秒
		}
		uni.setStorageSync(key, JSON.stringify(obj))
	}

	/**
	 *
	 * @param {缓存key} key
	 */
	getCache(key) {
		let val = uni.getStorageSync(key)
		if (!val) {
			return null
		}
		val = JSON.parse(val)
		if (val.expire && Date.now() / 1000 - val.time > val.expire) {
			uni.removeStorageSync(key)
			return null
		}
		return val.data
	}

	timeago(dateTimeStamp) {
		var minute = 1000 * 60; //把分，时，天，周，半个月，一个月用毫秒表示
		var hour = minute * 60;
		var day = hour * 24;
		var week = day * 7;
		var halfamonth = day * 15;
		var month = day * 30;
		var now = new Date().getTime(); //获取当前时间毫秒
		var diffValue = now - dateTimeStamp; //时间差

		if (diffValue < 0) {
			return;
		}
		var minC = diffValue / minute; //计算时间差的分，时，天，周，月
		var hourC = diffValue / hour;
		var dayC = diffValue / day;
		var weekC = diffValue / week;
		var monthC = diffValue / month;
		var result = ''
		if (monthC >= 1 && monthC <= 3) {
			result = " " + parseInt(monthC) + "月前"
		} else if (weekC >= 1 && weekC <= 3) {
			result = " " + parseInt(weekC) + "周前"
		} else if (dayC >= 1 && dayC <= 6) {
			result = " " + parseInt(dayC) + "天前"
		} else if (hourC >= 1 && hourC <= 23) {
			result = " " + parseInt(hourC) + "小时前"
		} else if (minC >= 1 && minC <= 59) {
			result = " " + parseInt(minC) + "分钟前"
		} else if (diffValue >= 0 && diffValue <= minute) {
			result = "刚刚"
		} else {
			var datetime = new Date();
			datetime.setTime(dateTimeStamp);
			var Nyear = datetime.getFullYear();
			var Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
			var Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
			var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
			var Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
			var Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
			result = Nyear + "-" + Nmonth + "-" + Ndate
		}
		return result;
	}

	/**
	 * 隐藏手机号敏感信息
	 */
	replaceTel(tel) {
		var reg = /^(\d{3})\d{4}(\d{4})$/;
		return tel.replace(reg, "$1****$2");
	}

	/**
	 * 检查是否注册，没注册跳转注册页面
	 */
	checkRegister() {
		let needRegister = $store.state.needRegister
		if (needRegister) {
			uni.redirectTo({
				url: "/pages/login/login"
			})
		}
	}
}

export default new Common()